package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.text.SimpleDateFormat;


/**
 * @autahor: csl
 * @date: 2019/7/27
 * @description:
 * @version:1.0
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private ReportService service;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
         try {
             //创建月份列表
             List<String>monthList= new ArrayList<>();
             Calendar calendar = Calendar.getInstance();
             calendar.add(Calendar.MONTH,-12);
             //构建一个simpleDateFormat
             SimpleDateFormat sdf = new SimpleDateFormat();
             for (int i = 0; i !=12 ; i++) {
                 calendar.add(Calendar.MONTH,1);
                 monthList.add(sdf.format(calendar.getTime()));
             }
             List<Integer> memberCountByMonthList = service.findMemberCountByMonthList(monthList);
             Map<String,Object>resultMap=new HashMap<>();
             resultMap.put("months",monthList);
             resultMap.put("memberCount",memberCountByMonthList);
             return new Result(true,MessageConst.ACTION_SUCCESS,resultMap);
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false, MessageConst.ACTION_FAIL);
                 }
    }
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
         try {
             List<Map<String, Object>> setmealCount = service.findSetmealCount();
             Map<String,Object>map=new HashMap<>();
            List<String>nameList= new ArrayList<>();
             for (Map<String, Object> setmealMap : setmealCount) {
                   nameList.add(setmealMap.get("name").toString());
             }
             map.put("setmealNames",nameList);
             map.put("setmealCount",setmealCount);
             return new Result(true,MessageConst.ACTION_SUCCESS,map);
         }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ACTION_FAIL);
                 }
    }
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
         try {
                  Map<String,Object>map=service.getBussinessReport();
                  return new Result(true,MessageConst.ACTION_SUCCESS,map);
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ACTION_FAIL);
                 }
    }
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
         try {
             Map<String, Object> result = service.getBussinessReport();
             // 取出返回结果
             String orderDate = (String)result.get("reportDate");
             // 获取会员相关数据
             Integer todayNewMember = (Integer)result.get("todayNewMember");
             Integer thisWeekNewMember = (Integer)result.get("thisWeekNewMember");
             Integer thisMonthNewMember = (Integer)result.get("thisMonthNewMember");
             Integer totalMember = (Integer)result.get("totalMember");
             // 获取预约相关数据
             Integer todayOrderNumber = (Integer)result.get("todayOrderNumber");
             Integer thisWeekOrderNumber = (Integer)result.get("thisWeekOrderNumber");
             Integer thisMonthOrderNumber = (Integer)result.get("thisMonthOrderNumber");
             // 获取到诊相关数据
             Integer todayVisitsNumber = (Integer)result.get("todayVisitsNumber");
             Integer thisWeekVisitsNumber = (Integer)result.get("thisWeekVisitsNumber");
             Integer thisMonthVisitsNumber = (Integer)result.get("thisMonthVisitsNumber");
             // 获取套餐数据
             List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
             InputStream stream = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
             //构建workbook对象
             XSSFWorkbook xssfWorkbook = new XSSFWorkbook(stream);
             //获取sheet页
             XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
             //获取报表日期行
             XSSFRow row = sheet.getRow(2);
             row.getCell(5).setCellValue(orderDate);
             //获取新增会员及会员总数行
             row = sheet.getRow(4);
             row.getCell(5).setCellValue(todayNewMember);
             row.getCell(7).setCellValue(totalMember);
             // 获取本周新增会员及本月新增会员行
             row = sheet.getRow(5);

             row.getCell(5).setCellValue(thisWeekNewMember);
             row.getCell(7).setCellValue(thisMonthNewMember);

             // 获取今日预约及到诊数行
             row = sheet.getRow(7);
             row.getCell(5).setCellValue(todayOrderNumber);
             row.getCell(7).setCellValue(todayVisitsNumber);

             // 获取本周预约及本周到诊行
             row = sheet.getRow(8);
             row.getCell(5).setCellValue(thisWeekOrderNumber);
             row.getCell(7).setCellValue(thisWeekVisitsNumber);

             // 获取本月预约及本月到诊行
             row = sheet.getRow(9);
             row.getCell(5).setCellValue(thisMonthOrderNumber);
             row.getCell(7).setCellValue(thisMonthVisitsNumber);
             // 获取热门套餐行，动态写入数据
             int rowStatNum=12;
             for (Map map : hotSetmeal) {
                 String name=(String)map.get("name");
                 Long setmealCount =(Long)map.get("setmeal_count");
                 BigDecimal proportion =(BigDecimal)map.get("proportion");
                 row=sheet.getRow(rowStatNum);
                 row.getCell(4).setCellValue(name);
                 row.getCell(5).setCellValue(setmealCount);
                 row.getCell(6).setCellValue(proportion.doubleValue());
                 rowStatNum++;
             }
             ServletOutputStream outputStream = response.getOutputStream();
             response.setContentType("application/vnd.ms-excel");
             response.setHeader("content-Disposition","attachment;filename="+orderDate+"_report.xlsx");
             xssfWorkbook.write(outputStream);
             outputStream.flush();
             outputStream.close();
             xssfWorkbook.close();
             return null;
         }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ACTION_FAIL);
                 }
    }
}
